package com.frxuexi.roport.view

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.UiState
import com.frxuexi.roport.ReportIntent
import com.frxuexi.roport.ReportViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch

@Composable
fun ReportPage(
    navHostController: NavHostController,
    json: String,
    vm: ReportViewModel = hiltViewModel()
) {
    val entity = Gson().fromJson(json, DialogEntity::class.java)
    Log.i("===", entity.toString())
    var nowTitle by remember {
        mutableStateOf(if(entity.type==1)"今日" else if(entity.type==2) "本周" else "本月")
    }
    var nextTitle by remember {
        mutableStateOf(if(entity.type==1)"明日" else if(entity.type==2) "下周" else "下月")
    }
    var isShow by remember {
        mutableStateOf(if(arrayOf(1,2,3).contains(entity.type)) true else false)
    }
    var todayValue by remember {
        mutableStateOf(if(isShow)"写出${nowTitle}工作内容" else "写出工作内容")
    }
    var nextValue by remember {
        mutableStateOf(if(isShow)"写出${nextTitle}工作计划" else "写出工作计划")
    }


    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        ToastUtils.showLong("提交成功")
                        navHostController.popBackStack()
                    }

                    else -> {}
                }
            }
        }
    })

    Column(
        Modifier
            .fillMaxSize()
            .background(colorResource(id = R.color.lightgray))
            .padding(20.dp)
    ) {
        Row(
            Modifier
                .fillMaxWidth(), verticalAlignment = Alignment.CenterVertically
        ) {
            BaseTitle(navHostController = navHostController, title = "日志编辑")
        }
        Spacer(modifier = Modifier.height(10.dp))
        val now = System.currentTimeMillis()
        Text(
            text = "${vm.toDate(now)} ${TimeUtils.getChineseWeek(now)}（今日）", Modifier
                .fillMaxWidth()
                .background(Color.White, RoundedCornerShape(5.dp))
                .padding(14.dp)
        )
        Spacer(modifier = Modifier.height(10.dp))
        if(isShow) {
            Text(
                text = "${nowTitle}内容", fontSize = 18.sp, modifier = Modifier
                    .fillMaxWidth()
                    .background(Color.White)
                    .border(1.dp, Color.LightGray)
                    .padding(14.dp)
            )
        }
        TextField(modifier = Modifier
            .height(100.dp)
            .fillMaxWidth()
            .onFocusChanged {
                if (it.isFocused) {
                    if (todayValue == "写出${nowTitle}工作内容")
                        todayValue = ""
                } else {
                    if (todayValue == "")
                        todayValue = "写出${nowTitle}工作内容"
                }
            }, value = todayValue, onValueChange = { todayValue = it },
            colors = TextFieldDefaults.colors(
                focusedContainerColor = Color.White,
                unfocusedContainerColor = Color.White,
                focusedIndicatorColor = Color.White,
                unfocusedIndicatorColor = Color.White
            )
        )
        Spacer(modifier = Modifier.height(10.dp))
        if(isShow) {
            Text(
                text = "${nextTitle}计划", fontSize = 18.sp, modifier = Modifier
                    .fillMaxWidth()
                    .background(Color.White)
                    .border(1.dp, Color.LightGray)
                    .padding(14.dp)
            )
        }
        TextField(modifier = Modifier
            .height(100.dp)
            .fillMaxWidth()
            .onFocusChanged {
                if (it.isFocused) {
                    if (nextValue == "写出${nextTitle}工作计划")
                        nextValue = ""
                } else {
                    if (nextValue == "")
                        nextValue = "写出${nextTitle}工作计划"
                }
            }, value = nextValue, onValueChange = { nextValue = it },
            colors = TextFieldDefaults.colors(
                focusedContainerColor = Color.White,
                unfocusedContainerColor = Color.White,
                focusedIndicatorColor = Color.White,
                unfocusedIndicatorColor = Color.White
            )
        )
        Spacer(modifier = Modifier.height(10.dp))
        Button(modifier = Modifier
            .fillMaxWidth(), onClick = {
            val map = mutableMapOf<String, Any>()
            map["reportType"] = entity.type
            map["reportTime"] = System.currentTimeMillis()
            map["reportRemind"] = "杨志涛"
            map["reportNowTitle"] = "${nowTitle}内容"
            map["reportNowValue"] = todayValue
            map["reportNextTitle"] = "${nextTitle}计划"
            map["reportNextValue"] = nextValue
            vm.sendIntent(ReportIntent.addReport(map))
        }) {
            Text(text = "提交")
        }
    }
}